package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.ByUserWedBudget;
import com.baseweb.webadmin.core.util.CheckCodeUtil;
import com.baseweb.webadmin.core.util.CommonUtil;
import com.baseweb.webadmin.core.util.ConfigUtil;
import com.baseweb.webadmin.service.UserService;
import net.sf.json.JSONObject;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description: 内容管理
 * @author: xiaoqiang
 * @time: 2017-08-28 15:23
 */
@Controller
@RequestMapping("/app/content/")
public class ContentAppController extends BaseController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/valid_code", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveIntentCustom(@RequestParam(value = "vcode") String vcode, ByUserWedBudget custom) {
        /**
         * 手机号码和发送的手机验证码一致性验证
         */
        String result = CheckCodeUtil.verify(custom.getMobile(), vcode);
        if ("1".equals(result)) {
            DateTime dtime = new DateTime();
            custom.setCreateTime(dtime);
            custom.setUpdateTime(dtime);
            userService.insertBudget(custom);
            return success(null, "成功");
        } else {
            return fail("手机号码和发送的手机验证码不一致");
        }
    }

    /**
     * 手机短信验证
     */
    @RequestMapping(value = "/sendValidMsgWeb", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> sendValidMsgWeb(@RequestParam(value = "mobile", required = true) String mobile){
        String code = CheckCodeUtil.newCheckCode(mobile);
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("code", code);
        map.put("product", ConfigUtil.getSmsFreeSignName());
        boolean flag = CommonUtil.sendMessage(mobile, JSONObject
                .fromObject(map).toString(), ConfigUtil
                .getSmsTemplateCodeRegist());
        if (flag) {
            return success("短信发送成功");
        } else {
            return fail("短信发送失败");
        }
    }
}
